<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script type="text/javascript" src="/layui/layui.js"></script>
    <style>
        .login-form {
            position: absolute;
            width: 100%;
            padding: 20px 25px;
            box-sizing: border-box;
        }

    </style>
    <title>Add Job</title>
</head>
<body>
<div class="layui-container">
    <div>
        <form class="layui-form login-form" lay-filter="data-form">
            <div class="layui-form-item">
                <label class="layui-form-label">Job Name</label>
                <div class="layui-input-block">
                    <input type="text" name="jobName" placeholder="Job Name" autocomplete="off" class="layui-input"
                           required
                           lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">爬虫间隔</label>
                <div class="layui-input-inline">
                    <input type="text" name="timeInterval" placeholder="timeInterval" autocomplete="off"
                           class="layui-input" required
                           lay-verify="number">
                </div>
                <div class="layui-input-inline">
                    <select name="unit" lay-verify="required">
                        <option value="second">秒(second)</option>
                        <option value="minute">分(minute)</option>
                        <option value="hour">时(hour)</option>
                        <option value="day">天(day)</option>
                        <option value="month">月(month)</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">重试次数</label>
                <div class="layui-input-inline">
                    <input type="text" name="retry" placeholder="Retry Times" autocomplete="off" class="layui-input"
                           required
                           lay-verify="number">
                </div>
                <div class="layui-form-mid layui-word-aux">爬虫任务发生异常后的重试次数，超过设定的次数后任务会停止。建议在3次左右。</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">URL</label>
                <div class="layui-input-block">
                    <input type="text" name="url" placeholder="url" autocomplete="off" class="layui-input" required
                           lay-verify="url">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">Method</label>
                <div class="layui-input-inline">
                    <select name="method" lay-verify="hm" required>
                        <option value="get">get</option>
                        <option value="put">put</option>
                        <option value="delete">delete</option>
                        <option value="post">post</option>
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">当前只支持POST/GET</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">请求参数</label>
                <div class="layui-input-block">
            <textarea type="text" name="params" placeholder="params" autocomplete="off"
                      class="layui-textarea" required lay-verify="jsonStr"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">请求头</label>
                <div class="layui-input-block">
            <textarea type="text" name="headers" placeholder="headers" autocomplete="off"
                      class="layui-textarea" required lay-verify="jsonStr"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="data-form" id="sub">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>

        </form>
    </div>

</div>
</body>
<script type="text/javascript">
    //Demo
    layui.use(['form', 'layer'], function () {
        const form = layui.form;
        const layer = layui.layer;
        const $ = layui.$;
        const methodArray = ['post', 'get', 'put', 'delete'];

        form.verify({
            jsonStr: function (value, item) { //value：表单的值、item：表单的DOM对象
                // console.log(value)
                if (value !== null && value.length > 0) {
                    try {
                        JSON.parse(value)
                    } catch (e) {
                        return '不是合法的json字符串';
                    }
                }
            },
            hm: function (value, item) {
                // console.log(value);

                let result = false;
                methodArray.forEach(x => {
                    if (value === x) {
                        result = true;
                    }
                })
                if (!result) {
                    return '不是合法的method类型';
                }
            }
        });

        //监听提交
        form.on('submit(data-form)', function (data) {
            // layer.msg(JSON.stringify(data.field))
            fetch("/addJob.do", {
                method: 'POST',
                body: JSON.stringify(data.field)
            }).then(res => res.json()).then(json => {
                console.log(json)
                if (json.code !== 0) return layer.msg('修改失败', {icon: 5})
                layer.msg("新增任务成功，页面即将跳转", {icon: 6})
                setTimeout(function () {
                    let index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.location.reload();
                }, 1000)
            }).catch(err => {
                console.log("error", err)
                layer.msg("服务器异常", {icon: 5})
            })
            return false;
        });

        document.onkeydown = function (e) {
            if (e.keyCode === 13 || e.which === 13) {
                $("#sub").click()
            }
        }
    });
</script>
</html>